<?php
# header of each page
function head($title='', $meta_description='', $meta_keywords='', $active='') {
header("Content-type: text/html; charset=UTF-8");
?>
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
<meta name="description" content="<?=$meta_description?>">

<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/reset.css">
<link rel="stylesheet" type="text/css" href="<?=HTML_BASE?>css/style.css">
<script src="<?=HTML_BASE?>js/modernizr.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript" src="<?=HTML_BASE?>js/base64.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
</head>

<body>
<div id="wrap">
	<div id="wrapin">
		
		<div id="top">
			<div id="logo">
				<a class="<?=$_SESSION['lang']?>" href="<?=HTML_BASE.$_SESSION['lang']?>"></a>
			</div>
			
			<div id="topright">
				
				<div id="toprighttop">
					<div id="search">
						<form id="search-form" action="" method="post" >
							<input id="search-text" maxlength="50" type="text" value="<?=$_SESSION['search']?>" onkeypress="return runScript(event);" />
							<a href="#" class="search_btn" onClick="sendform(); return false;"  ><?=__SEARCH?></a> 
						</form>
					  <script type="text/javascript">
					  
						function runScript(e) {
							if (e.keyCode == 13) {
								sendform();
								return false;
							}
						}
						function sendform(){
							var str = $('#search-text').val();
							
							
							if(str.length < 3)
							{
								$('#search-text').addClass('redborder');
							}else{
								
								
								$('#search-form').attr('action','<?=HTML_BASE.$_SESSION['lang']?>/search/'+str);
								$('#search-form').submit();
								
							}
							
						};
					  </script>
					</div>
					<div id="lang">
						Language:
						<div id="langchange">
							<span></span>
                            
                            <?php
							$sql = 'SELECT lang_id,Name,code FROM language WHERE published';
							$result_lang = query($sql);
							$noact = '';
							$hiper = '';
							if($_GET['prm1']) $hiper .= '/'.$_GET['prm1'];
							if($_GET['prm2']) $hiper .= '/'.$_GET['prm2'];
							while ($row_lang = mysql_fetch_object($result_lang)) {
									
									
									if($row_lang->lang_id == $_SESSION['lang_id'])
									{
										 $act = '<a class="current" href="'.HTML_BASE.$row_lang->code.$hiper.'">'.$row_lang->Name.'</a> ';
									}else{
										$noact .= '<a  href="'.HTML_BASE.$row_lang->code.$hiper.'">'.$row_lang->Name.'</a> ';
									}
									
									
													
							}
							echo $act.$noact;
							
							?>
														
						</div>
					</div>
				</div>
				
              
            
				<div id="menu">
					<?php
			   
			 
			 $sql= "SELECT menuitems FROM menus WHERE menu_id = '9'";
			 $result = query($sql);
			 $row_m = mysql_fetch_object($result);
			 
			 $arr =  json_c_decode($row_m->menuitems);
			 function create_ul($ar,$first)
			 {
					$menu_id = 9;
					echo '<ul>';
					
					
					foreach ($ar as $key => $value) {
					 
					
						$item = $value['id'];
						
						$sqlm = "SELECT parent_id,title FROM  menu_description 
							  WHERE menu_id = '".$menu_id."'AND item_id = '".$item."'
							  AND lang_id = '".$_SESSION['lang_id']."'
							 ";
					
						$rowm = mysql_fetch_object(query($sqlm)); 
					
						$sql_sef = "SELECT sef_url FROM  pages 
							  WHERE page_id = '".$rowm->parent_id."'
							 ";
						$row_sef = mysql_fetch_object(query($sql_sef));
						
						$active = '';
						if($row_sef->sef_url == $_GET['prm1']) $active = 'class="active"';
						$parent = '';
						if($value['children'])$parent = 'class="isparent"';
						
						$href = 'href="#"';
						if(!$value['children']) $href = 'href="'.HTML_BASE.$_SESSION['lang'].'/'.$row_sef->sef_url.'"';
						
						
						
						echo '<li '.$parent.'><a '.$active.' '.$href.' >'.$rowm->title.'</a>';
						 
						if($value['children']) create_ul($value['children'],false);
						 
						echo '</li>';
					}
						
						
						echo '</ul>';
					
					
			 }
			 
			 create_ul($arr,true);
					
					
					?>
				</div>
				
			</div>
		</div><!--#top-->
	
<?php	
}

# footer of each page
function foot($tmp='') {
?>
		
	
	</div>
</div>
<div id="footerwrap">
	<div id="footer">
		<div id="footleft">
			<h3><a href="<?=HTML_BASE.$_SESSION['lang']?>/news"><?=__LATEST_NEWS?></a></h3>
			
            
            <?php
            
				$sql = "SELECT
				c.sef_url,
				c.creation_time,
				
				d.title,
				d.description
				
			FROM
				news AS c,
				news_description AS d
			WHERE
				c.new_id = d.new_id
				AND
				d.lang_id = '".$_SESSION['lang_id']."'
				AND c.published
				
				ORDER BY c.new_id DESC  LIMIT 2
			  ";
			  $result = query($sql);
			   while ($row = mysql_fetch_object($result)) {
			?>
            
			<div class="new">
				<div class="title"><a href="<?=HTML_BASE.$_SESSION['lang']?>/news/<?=$row->sef_url?>"><?=$row->title?></a></div>
				<div class="date"><?=date('d.m.Y', strtotime($row->creation_time))?></div>
				<div class="content">
			<p><?=text_format($row->description, 200)?>...</p>
				</div>
				<a class="readmore" href="<?=HTML_BASE.$_SESSION['lang']?>/news/<?=$row->sef_url?>"><?=__Read_more?></a>
			</div>
			<?php }?>
			
			
			
		</div>
		<div id="footright">
        
         <?php
            
				$sql = "SELECT
				
				d.title,
				d.description
				
			FROM
				pages AS c,
				page_description AS d
			WHERE
				c.page_id = d.page_id
				AND
				d.lang_id = '".$_SESSION['lang_id']."'
				AND c.sef_url = 'contacts'
				
			  ";
			  $result = query($sql);
			  $row = mysql_fetch_object($result);
			?>
			<h3><a href="<?=HTML_BASE.$_SESSION['lang']?>/contacts"><?=$row->title?></a></h3>
			
			<?=stripslashes($row->description)?>
			
			<div class="social">
				<a class="fb" href="#"></a>
				<a class="tw" href="#"></a>
				<a class="sk" href="#"></a>
			</div>
            
            <div class="payments">
            	<img src="<?=HTML_BASE?>images/payment-paypal.png" alt="PayPal">
                <img src="<?=HTML_BASE?>images/payment-visa.png" alt="Visa">
                <img src="<?=HTML_BASE?>images/payment-mc.png" alt="Master Card">
                <img src="<?=HTML_BASE?>images/payment-mb.png" alt="MULTIBANCO">
                <br>
                <img src="<?=HTML_BASE?>images/comodo_secure.png" alt="Combo Secure">
            </div>
            
		</div>
		<div id="footbot">
			<div id="copy">
				Copyright © <?=date("Y")?> OLAPARK. All rights reserved.
			</div>
			<div id="footmenu">
				<?php
                
			
		
		 
		 
		 $sql= "SELECT menuitems FROM menus WHERE menu_id = '10'";
		 $result = query($sql);
		 $row_m = mysql_fetch_object($result);
		 
	
		 function create_a($ar,$first)
		 {
				$menu_id = 10;
				foreach ($ar as $key => $value) {
				 
				
					$item = $value['id'];
					
					$sqlm = "SELECT parent_id,title FROM  menu_description 
						  WHERE menu_id = '".$menu_id."'AND item_id = '".$item."'
						  AND lang_id = '".$_SESSION['lang_id']."'
						 ";
				
					$rowm = mysql_fetch_object(query($sqlm)); 
				
					$sql_sef = "SELECT sef_url FROM  pages 
						  WHERE page_id = '".$rowm->parent_id."'
						 ";
					$row_sef = mysql_fetch_object(query($sql_sef));
					
					echo '<a href="'.HTML_BASE.$_SESSION['lang'].'/'.$row_sef->sef_url.'">'.$rowm->title.'</a>';
					 
					
				}
					
				
				
		 }
		 
		 create_a(json_c_decode($row_m->menuitems),true);
				
				
				?>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript" language="javascript" src="<?=HTML_BASE?>js/script.js" ></script>
</body>
</html>
        
<?php
}

# a redirecting function that adds the session ID if cookies are disabled and stops the execution of the script
function redirect($location) {
	if (!$location) error("Missing arguments.");

	$a = parse_url($location);
	if (!isset($a['query'])) {
		$a['query'] = '';
	}
	
	$session_delimiter = ($a['query'])?'&':'?';
	header("Location: $location".addsession($session_delimiter));
	exit;
}

# adding the session_id when cookies are disabled
function addsession($symbol="?") {
	return (!isset($_COOKIE[session_name()])&&session_id())?$symbol.session_name()."=".session_id():"";
}

function escape($string,$encoding=false) {
  return mysql_real_escape_string( (($encoding)?encoding($string):$string) );
}

function encoding($string) {
  return iconv('ISO-8859-1', 'UTF-8', $string);
}

function sth($string) {
  return htmlspecialchars(stripslashes($string));
}

# checks whether an email is valid or not
function checkEmail($email) {
	return preg_match("/^[A-z0-9\.\-]+@(?:[A-z0-9\-]+\.)+[A-z0-9]{1,6}$/", $email);
}

# Cut a text to specified chracters length
function text_format($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, ' ', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}

function text_format_blog($text, $length) {
	$str = '';
	$text_length = mb_strlen($text, "UTF-8");
	 
	if ($text_length < $length) {
		$str = $text;		
	} else {
		$position_end = strpos($text, '<hr />', $length);
		$str = substr($text, 0, ($position_end)?$position_end:$text_length);
	}
	
	return $str;
}


# general error function
function error($s="An error occured.") {
  if (!headers_sent()) {
    head("Error");
    echo "<table cellpadding=4 width=".SITE_WIDTH." align=center border=0><tr><td align=center class=title>$s</td></tr></table>";
    foot();
  }
  else
    echo $s;
  exit;
}

# debug function for dumping a variable value
function pp($var, $stop=false) {
  echo "<pre>";
  print_r($var);
  echo "</pre>";
  if ($stop) exit;
}

# Generate numbers
function generateNumber () {

  # start with a blank number
  $number = "";

  # define possible characters
  $possible = "0123456789"; 

  # pick a random character from the possible ones
  $number = substr($possible, mt_rand(0, strlen($possible)-1), 1);

  # done!
  return $number;

}



function json_c_decode($json, $assoc = true, $depth = 512, $options = 0) {
			
				$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
				
				if(version_compare(phpversion(), '5.4.0', '>=')) {
					$json = json_decode($json, $assoc, $depth, $options);
				}
				elseif(version_compare(phpversion(), '5.3.0', '>=')) {
					$json = json_decode($json, $assoc, $depth);
				}
				else {
					$json = json_decode($json, $assoc);
				}
			
				return $json;
		}



?>